//
//  BuyViewController.swift
//  CompareToBuy
//
//  Created by 唐先才 on 16/1/26.
//  Copyright © 2016年 唐先才. All rights reserved.
//

import Foundation
import UIKit

class BuyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    var _tableView: UITableView?
    var _dataArray: NSMutableArray = []
    var _page: NSInteger = 0
    var _isPulling: Bool?
    var waitView: WaitLoadView?
    
    override func viewWillAppear(animated: Bool) {
        self.view.backgroundColor = UIColor.greenColor()
    }
    
    override func viewDidLoad() {
        self._page = 1
        self.createData()
        self.createTableView()
        self.createRefresh()
        waitView = WaitLoadView(frame: self.view.frame)
        guard let waitView = self.waitView else {
            return
        }
        self.view.addSubview(waitView)
    }
    
    func createData() {
        if _isPulling == true {
            self._dataArray.removeAllObjects()
        }
        let manager = AFHTTPRequestOperationManager()
        let parameters = ["brightUrl": "",
                          "catUrl": "",
                          "exceptMallUrl": "",
                          "homeShow": "0",
                          "infoNation": "0",
                          "infoType": "5",
                          "isTop": "1",
                          "keyWord": "",
                          "mallUrl": "",
                          "pageIndex": self._page,
                          "pageSize": "20",
                          "tagUrl": "",
                         ]
        manager.POST(buy_url, parameters: parameters, success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
            guard let waitView = self.waitView else {
                return
            }
            waitView.removeFromSuperview()
            guard let array = responseObject as? [[String: AnyObject]] else {
                return
            }
            for dict: [String: AnyObject] in array {
                let model = TxcModel.modelWithDict(dict)
                self._dataArray.addObject(model)
            }
//            print(self._dataArray)
            guard let _tableView = self._tableView else {
                return
            }
            _tableView.reloadData()
            }) { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
        }
    }
    
    func createTableView() {
        _tableView = UITableView(frame: self.view.frame, style: .Plain)
        guard let _tableView = _tableView else {
            return
        }
        _tableView.dataSource = self
        _tableView.delegate = self
        self.view.addSubview(_tableView)
        _tableView.rowHeight = 130
        _tableView.showsVerticalScrollIndicator = false
        let nib = UINib(nibName: "BuyCell", bundle: NSBundle.mainBundle())
        _tableView.registerNib(nib, forCellReuseIdentifier: "buyCell")
    }
    
 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self._dataArray.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let buyCell = BuyCell.buyCellWith(tableView, dataArray: self._dataArray, indexPath: indexPath)
        return buyCell
    }
    
    ///创建刷新
    func createRefresh() {
        guard let _tableView = self._tableView else {
            print("创建刷新失败")
            return
        }
        _tableView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: "downRefresh")
        _tableView.mj_footer = MJRefreshAutoFooter(refreshingTarget: self, refreshingAction: "upRefresh")
    }
    
    func downRefresh() {
        guard let _tableView = self._tableView else {
            print("下拉刷新失败")
            return
        }
        _isPulling = true
        _tableView.mj_header.beginRefreshing()
        self.createData()
        _tableView.mj_header.endRefreshing()
    }
    
    func upRefresh() {
        guard let _tableView = self._tableView else {
            print("上载刷新失败")
            return
        }
        _isPulling = false
        self._page++
        self.createData()
        _tableView.mj_footer.endRefreshing()
    }
}